/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.daoImpl;

import br.com.deployment.dao.ResponseDAO;
import br.com.deployment.entities.Category;
import br.com.deployment.entities.QuestionnaireResponse;
import br.com.deployment.entities.Response;
import br.com.deployment.genericdao.GenericDAO;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author ely
 */
@Stateless
@LocalBean
public class ResponseDAOImpl extends GenericDAO<Response> implements ResponseDAO{

    public ResponseDAOImpl(){
        super(Response.class);
    }
    
    @Override
    public List<Response> findResponse(Response response) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Response> findResponseQResponse(QuestionnaireResponse qResponse) {
        String sql = "SELECT r FROM QuestionnaireResponse q JOIN q.response r "
                   + "JOIN r.optionses o WHERE "
                   + "q.id = :id";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", qResponse.getId());
        return (List<Response>) query.getResultList();
    }

    @Override
    public List<Response> findResponseQuestionnaireResponse(QuestionnaireResponse qResponse) {
         String sql = "SELECT r FROM QuestionnaireResponse q JOIN q.response r "
                   +  "WHERE q.id = :id";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", qResponse.getId());
        List<Response> results = (List<Response>) query.getResultList();
        System.err.println(results.size());
        return results;
    }

    @Override
    public List<Response> findResponseCategory(Category c, QuestionnaireResponse q) {
        String sql = "SELECT r FROM QuestionnaireResponse q JOIN q.response r JOIN r.issues i "
                   +  "WHERE i.category.id = :id AND q.id = :idI ORDER BY r.id ASC";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", c.getId());
        query.setParameter("idI",q.getId());
        return (List<Response>) query.getResultList();
    }

   
    
}
